<template>
        <div class="backgroud">
            <mt-header fixed title="修改密码" style="z-index: 999999;">
            <span slot="left" @click="goBack">
                <mt-button icon="back" slot="left">返回</mt-button>
            </span>
        </mt-header>
                <div class="mui-content">
                        <div class="mui-input-group">
                            <div class="mui-input-row">
                                <input id='telephone' type="password" class="mui-input-clear mui-input" placeholder="请输入旧密码" v-model="oldPassword">
                            </div>
                            <div class="mui-input-row">
                                <input type="password" class="mui-input-clear mui-input" placeholder="请输入新密码" v-model="newPassword">
                            </div>
                            <div class="mui-input-row">
                                <input type="password" class="mui-input-clear mui-input" placeholder="确认新密码" v-model="requertPassword">
                            </div>
                        </div>
    
                        <div class="mui-content-padded">
                            <button id='reg' class="mui-btn mui-btn-block mui-btn-primary" @click="changePassword">立即修改</button>
                        </div>
                </div>
                </div>
    </template>
    
    <script>
		import CryptoJS from 'crypto-js'
		import {Toast} from 'mint-ui'
		export default{
			data() {
				return {
					oldPassword:'',
					newPassword:'',
					requertPassword:'',
					sessionId:'',
				}
			},
			created() {
				this.sessionId = localStorage.getItem('sessionId');
			},
			methods: {
                goBack() {
                    //点击后退
                    this.$router.go(-1);
                },
				changePassword(){
					var oldPassword = this.set(this.oldPassword,'CYDCxtaesmy2046j');
                    var newPassword = this.set(this.newPassword,'CYDCxtaesmy2046j');
					if(this.requertPassword!=this.newPassword){
                        Toast("两次输入密码不一致");
                        return;
					}
					this.axios.post('http://zxdc.catsic.com:9090/sampling/user/updatepwd',{'data':{"oldPassword":oldPassword,"newPassword":newPassword}},{
                         'headers':{
                             'Content-Type':'application/json',
                             'mobileHeader':'{"sessionId":'+this.sessionId+'}'
                            }
                            }).then(result=>{
                                if(result.data.status == "1000"){ 
										Toast("修改成功,请重新登录");
										this.outLogin();
                                }else if(result.data.status == "4001"||result.data.status == "5005"||result.data.status == "1002"){
                                    this.dateFlag = true;
                                    this.sessionId = "";
                                    localStorage.setItem('sessionId', JSON.stringify(this.sessionId));
                                    this.$store.commit("outLogin");
                                    this.msg = result.data.msg;
                                    Toast(this.msg);
                                    this.$router.push({
                                        path:'/login'
                                    });
                                }else{
                                    this.msg = result.data.msg;
                                    Toast(this.msg);
                                    return;
                                }
                        console.log(result);
                     });  
				},
				set(word, keyStr){ 
                    keyStr = keyStr ? keyStr : 'CYDCxtaesmy2046j';   //16位的密钥，自己定义，和下面的密钥要相同
                    var key  = CryptoJS.enc.Utf8.parse(keyStr);
                    var srcs = CryptoJS.enc.Utf8.parse(word);
                    var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
                    return encrypted.toString();
                },
                //解密
                get(word, keyStr){  
                    keyStr = keyStr ? keyStr : 'CYDCxtaesmy2046j';
                    var key  = CryptoJS.enc.Utf8.parse(keyStr);
                    var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
                    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
				},
				outLogin(){
                    this.dateFlag = true;
                    this.axios.post('http://zxdc.catsic.com:9090/sampling/user/logout',{'data':{}},{
                         'headers':{
                             'Content-Type':'application/json',
                             'mobileHeader':'{"sessionId":'+this.sessionId+'}'
                            }
                            }).then(result=>{
                                if(result.data.status == "1000"){
                                    this.sessionId = "";
                                    localStorage.setItem('sessionId', JSON.stringify(this.sessionId));
                                        this.$store.commit("outLogin");
                                        this.$router.push({
                                            path:'/login'
                                        });
                                }else if(result.data.status == "4001"||result.data.status == "5005"||result.data.status == "1002"){
                                    this.dateFlag = true;
                                    this.sessionId = "";
                                    localStorage.setItem('sessionId', JSON.stringify(this.sessionId));
                                    this.$store.commit("outLogin");
                                    this.msg = result.data.msg;
                                    Toast(this.msg);
                                    this.$router.push({
                                        path:'/login'
                                    });
                                }
                     });
                    },
			},
		}
    </script>
    
    <style lang="scss" scoped>
        .mui-btn {
				padding: 10px;
				height: 100%;
			}
			
			.mui-input-group {
				background: transparent;
				margin-left: 8%;
				margin-right: 8%;
				border: none !important;
			}
			
			.mui-input-group:after {
				height: 0px;
			}
			
			.mui-input-group:before {
				height: 0px;
			}
			
			.mui-input-group:first-child {
				margin-top: 20px;
			}
			
			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}
			
			.mui-input-row label {
				float: right;
				width: 50%;
				font-size: 15px;
			}
			
			.mui-input-row label~input {
				float: left;
				width: 40%;
				padding-left: 15px;
			}
        
    </style>